<?php
namespace Admin\Controller;
class UserPicController extends CommonController{
    public function _initialize() {
		parent::_initialize();
    }

    public function _before_index() {
        $p = I('p',1,'intval');
        $this->assign('p',$p);

    }
    protected function _search() {
        $map = array();
       ($keyword = I('get.keyword')) && $map['truename|qq'] = array('like', '%'.$keyword.'%');
        $cate_id = I('get.cate_id');
		if( $_GET['status']==null ){
            $status = -1;
        }else{
            $status = intval($_GET['status']);
        }
        $status>=0 && $map['status'] = array('eq',$status);
        $selected_ids = '';
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id'=>$cate_id))->getField('spid');
            $selected_ids = $spid ? $spid . $cate_id : $cate_id;
        }

        $this->assign('search', array(
            'cate_id' => $cate_id,
            'selected_ids' => $selected_ids,
            'status'  => $status,
            'keyword' => $keyword,
        ));
        return $map;
    }

    protected function _before_insert($data){
        $data['encode']    = codestr();
        $data['password']  = md5(md5($data['password']).$data['encode']);
        return $data;
    }

    //执行编辑页面
    public function _before_edit(){
        $id = I('get.id');
        $userinfo = D("UserInfo")->where(array('uid'=>$id))->find();
        //$article = $this->_cate_mod->field('id,cate_id')->where(array('uid'=>$userinfo['id']))->find();
        $spid = $this->_cate_mod->where(array('id'=>$userinfo['cate_id']))->getField('spid');
        $account = D("UserAccount")->where(array('uid'=>$id))->find();
        if( $spid==0 ){
            $spid = $userinfo['cate_id'];
        }else{
            $spid .= $userinfo['cate_id'];
        }
        $this->assign('uinfo',$userinfo);
        $this->assign('selected_ids',$spid);
        $this->assign('account',$account);
    }

    //执行更新
    protected function _before_update($data) {
       if(empty($data['password'])){
           unset($data['password']);
       }else{
           $data['encode']    = codestr();
           $data['password']  = md5(md5($data['password']).$data['encode']);
       }
        return $data;
    }

    //更新成功返回
    protected function _after_update($id){
        $info = $_POST['info'];
        $obj = D("UserInfo");
        $obj->where(array('uid'=>$id))->save($info);
        $account = $_POST['account'];
        if(!empty($account['point'])){
            switch($account['pointtype']){
                case 'setinc':
                    $record['num'] = $account['point'];
                    $record['title'] = '管理员增加积分'.$account['point'];
                    break;
                case 'setdesc':
                    $record['num'] = -1*$account['point'];
                    $record['title'] = '管理员减少积分'.$account['point'];
                    break;
            }
            $record['memo'] = '管理员操作';
            Record($id,$record);
        }
        if(!empty($account['money'])){
            switch($account['moneytype']){
                case 'setinc':
                    $record['num'] = $account['money'];
                    $record['title'] = '管理员增加余额'.$account['money'];
                    break;
                case 'setdesc':
                    $record['num'] = -1*$account['money'];
                    $record['title'] = '管理员减少余额'.$account['money'];
                    break;
                case 'freeze':
                    $useraccount = $this->getUserBlance($id);
                    if($useraccount['blance']<$account['money']){
                        $this->error('可冻结余额最多为：'.$useraccount['blance']);
                    }
                    $record['num'] = -1*$account['money'];
                    $record['title'] = '管理员冻结余额'.$account['money'];
                    D("UserAccount")->where(array('uid'=>$id))->setInc('freeze',abs($record['num']));
                    break;
                case 'unfreeze':
                    $useraccount = $this->getUserBlance($id);
                    if($useraccount['freeze']<$account['money']){
                        $this->error('可解冻余额最多为：'.$useraccount['freeze']);
                    }
                    $record['num'] = $account['money'];
                    $record['title'] = '管理员解冻余额'.$account['money'];
                    D("UserAccount")->where(array('uid'=>$id))->setDec('freeze',abs($record['num']));
                    break;
            }
            $record['memo'] = '管理员操作';
            Record($id,$record,2);
        }
    }



}